<?php
class Pages_model extends CI_Model 
{
	var $fields = array('name','content');
	var $required_fields = array('name');
	//var $table_name = 'users';
	var $table_name = 'pages';
    function pages_model()
    {
        parent::__construct();
    }
    
    function getpages($start = 0, $limit = 10,$where = array('deleted' => 0))
	{
    	$this->db->order_by('id DESC');
    	$this->db->where($where);
    	$query = $this->db->get($this->table_name,$limit,$start);
		$records = $query->result_array();

		$this->db->select('count(id) as total');
    	$this->db->where($where);
    	$rst = $this->db->get($this->table_name);
		$total = $rst->first_row()->total;
		
		$obj->records = $records;
		$obj->total = $total;
		return $obj;
    }
    
    
	function required($data = array()) {
    	if(empty($data)) return false;
    	$this->obj = get_instance();
    	$status = $this->obj->retrieve->required($this->required_fields, $data);
    	return $status;
    }
    
	function add($data = array()){
    	if(empty($data)) return -1;
    	if(!empty($data['mypassword']))
    		$data['mypassword'] = md5($data['mypassword']);
    	else 
    		$data['mypassword'] = '';
    	if(!$this->checkUniqueUsername($data['username'])) return -3;	
    	if($this->required($data)) {
			$status = $this->db->insert($this->table_name, $data);
			return $status;
    	} else {
    		return -2;
    	}
    }
    
    function udpate($id = '', $data = array()){
		//error_reporting(E_ALL);
    	if(empty($data) || empty($id)) return -1;
    	
    	if($this->required($data)) {
    		$this->db->where(array('id' => $id));
			$status = $this->db->update($this->table_name, $data);
			return $status;
    	} else {
    		return -2;
    	}
    }
	function export($content = '', $filename = '') 
	{
		//$filename= "application/views/".$filename;
		if(empty($content) || empty($filename)) return;
		if(chmod($filename, 0777)) {
		$size = file_put_contents($filename,$content);
		chmod($filename, 0755);
		return $size;
		}
		else {
		return false;
		}
	}

    
    function delete($id = ''){
		if(empty($id)) return -1;
		$data['deleted'] = 1;
		$this->db->where(array('id' => $id));
		$status = $this->db->update($this->table_name, $data);
		return $status;
    }
 
}
?>